<template>
  <PageContainer>
    <div class="login-container">
      <GlassCard class="login-card">
        <template #header>
          <div class="login-header">
            <div class="login-logo">
              <img src="@/assets/logo.png" alt="Logo" class="logo-image" />
            </div>
            <h2 class="login-title">AI 应用生成平台</h2>
            <p class="login-subtitle">不写一行代码，生成完整应用</p>
          </div>
        </template>

        <a-form
          :model="formState"
          name="basic"
          autocomplete="off"
          @finish="handleSubmit"
          layout="vertical"
          class="login-form"
        >
          <a-form-item
            label="账号"
            name="userAccount"
            :rules="[{ required: true, message: '请输入账号' }]"
          >
            <a-input
              v-model:value="formState.userAccount"
              placeholder="请输入账号"
              size="large"
            />
          </a-form-item>

          <a-form-item
            label="密码"
            name="userPassword"
            :rules="[
              { required: true, message: '请输入密码' },
              { min: 6, message: '密码长度不能小于 6 位' },
            ]"
          >
            <a-input-password
              v-model:value="formState.userPassword"
              placeholder="请输入密码"
              size="large"
            />
          </a-form-item>

          <div class="form-tips">
            没有账号？
            <RouterLink to="/user/register" class="register-link">立即注册</RouterLink>
          </div>

          <a-form-item>
            <GlassButton
              type="primary"
              size="large"
              :loading="submitting"
              html-type="submit"
              class="login-button"
            >
              {{ submitting ? '登录中...' : '登录' }}
            </GlassButton>
          </a-form-item>
        </a-form>
      </GlassCard>
    </div>
  </PageContainer>
</template>
<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import { userLogin } from '@/api/userController.ts'
import { useLoginUserStore } from '@/stores/loginUser.ts'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import PageContainer from '@/components/base/PageContainer.vue'
import GlassCard from '@/components/base/GlassCard.vue'
import GlassButton from '@/components/base/GlassButton.vue'

const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
})

const submitting = ref(false)
const router = useRouter()
const loginUserStore = useLoginUserStore()

// 检查登录状态
onMounted(async () => {
  // 如果已经登录，直接跳转到首页
  if (loginUserStore.loginUser?.id) {
    await router.push('/')
    return
  }

  // 尝试获取登录用户信息
  try {
    await loginUserStore.fetchLoginUser()
    if (loginUserStore.loginUser?.id) {
      await router.push('/')
    }
  } catch (error) {
    // 获取失败说明未登录，继续显示登录页面
    console.log('未登录，显示登录页面')
  }
})

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: API.UserLoginRequest) => {
  if (submitting.value) return

  submitting.value = true
  try {
    console.log('登录参数:', values)
    const res = await userLogin(values)
    console.log('登录响应:', res)
    // 登录成功，把登录态保存到全局状态中
    if (res.data.code === 0 && res.data.data) {
      await loginUserStore.fetchLoginUser()
      message.success('登录成功')
      router.push({
        path: '/',
        replace: true,
      })
    } else {
      message.error('登录失败：' + res.data.message)
    }
  } catch (error) {
    console.error('登录错误:', error)
    message.error('登录失败，请稍后重试')
  } finally {
    submitting.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 80vh;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
}

.login-header {
  text-align: center;
  margin-bottom: 8px;
}

.login-logo {
  margin-bottom: 16px;
}

.logo-image {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  box-shadow: 0 4px 16px rgba(59, 130, 246, 0.3);
}

.login-title {
  margin: 0 0 8px;
  font-size: 28px;
  font-weight: 700;
  background: linear-gradient(135deg, #ffffff 0%, #e2e8f0 50%, #cbd5e1 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.login-subtitle {
  margin: 0;
  color: rgba(255, 255, 255, 0.7);
  font-size: 16px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.login-form {
  margin-top: 32px;
}

.login-form :deep(.ant-form-item-label > label) {
  color: rgba(255, 255, 255, 0.9);
  font-weight: 500;
}

.login-form :deep(.ant-input),
.login-form :deep(.ant-input-password) {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  color: #ffffff;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

/* 密码输入框内部的input元素 */
.login-form :deep(.ant-input-password .ant-input) {
  background: transparent !important;
  border: none !important;
  color: #ffffff;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  box-shadow: none !important;
}

.login-form :deep(.ant-input::placeholder),
.login-form :deep(.ant-input-password input::placeholder) {
  color: rgba(255, 255, 255, 0.5);
}

.login-form :deep(.ant-input:focus),
.login-form :deep(.ant-input-password:focus),
.login-form :deep(.ant-input-focused),
.login-form :deep(.ant-input-password-focused) {
  border-color: #60a5fa;
  box-shadow: 0 0 0 2px rgba(96, 165, 250, 0.2);
  background: rgba(255, 255, 255, 0.15);
}

.login-form :deep(.ant-input-password-icon) {
  color: rgba(255, 255, 255, 0.6);
}

.form-tips {
  text-align: center;
  color: rgba(255, 255, 255, 0.6);
  font-size: 14px;
  margin-bottom: 24px;
}

.register-link {
  color: #60a5fa;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.register-link:hover {
  color: #93c5fd;
  text-decoration: underline;
}

.login-button {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 16px;
    min-height: 70vh;
  }

  .login-title {
    font-size: 24px;
  }

  .login-subtitle {
    font-size: 14px;
  }

  .logo-image {
    width: 56px;
    height: 56px;
  }
}
</style>
